/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

import java.util.Map;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.lmx.common.constants.CommonConstants;
import com.lmx.common.context.ActionContextHolder;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.RoomService;
import com.lmx.persistence.service.WorkPlaceService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.Device;
import com.lmx.repoDomain.tmodel.Room;

@JsonApiResourceRepository(Room.class)
@Component
public class RoomRepository extends BaseRepository<Room> {

	@Autowired
	private WorkPlaceService workPlaceService;
	
	@Autowired
	private RoomService roomService;
	
	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(Room.class);
	}

	@Override
	@JsonApiFindAll
	public Iterable<Room> findAll(final QueryParams requestParams) {
		Map<String, String> filter = CommonUtil.getKeyMapByFilter(requestParams);
		if (filter.containsKey("forTenant")
				&& CommonConstants.DEFAULT_STATUS_YES_1.toString().equals(filter.get("forTenant"))) {
			Long tenantId = ActionContextHolder.getContext().getTenantId();
			return this.roomService.findNotDelete("tenant.id", tenantId);
		}
		return super.findAll(requestParams);
	}

	@Override
	@JsonApiSave
	public Room save(Room record) {

		if (record.getId() == null) {
			dslJpaService.create(record);
		} else {
			if (record.getDelStatus() == 1) {// 删除
				workPlaceService.deleteRoom(record);
			} else {
				//公共区域绑定scanner
				if (record.getScannerID() != null && !"".equals(record.getScannerID()) && record.getScannerID().matches("\\d+")) {
					Room room=workPlaceService.getRoom(record);
					dslJpaService.setEntityClass(Device.class);
					Device scanner=(Device) dslJpaService.findById(Long.parseLong(record.getScannerID()));
					dslJpaService.setEntityClass(Room.class);
					if ((room.getScanner() != null && !record.getScannerID().equals(room.getScanner().getId().toString()))
							|| (room.getScanner() == null)) {
						if (scanner.getBindStatus() != null && scanner.getStatus() != null
								&& CommonConstants.DICTTYPE.DEVICE_BIND_STATUS_1.label().equals(scanner.getBindStatus().getTypecode())
								&& CommonConstants.DICTTYPE.DEVICE_STATUS_2.label().equals(scanner.getStatus().getTypecode())) {
							throw new BusinessException(null,
									CommonConstants.ERROR_CODE.ADD_ERROR.label() + "-" + scanner.getSeq(), null);
						}else{
							record.setScanner(room.getScanner());
							return workPlaceService.publicAddDevice(record);
						}
					}
				}
			}
			dslJpaService.update(record);
		}
		return record;
	}

}
